using System;
using Deliveryroomstatusbulletin.EntityDAO;
using Deliveryroomstatusbulletin.EntityData;
using Skynet.Data;
using Skynet.ExceptionManagement;
using SystemFramework.NewCommon;

namespace Deliveryroomstatusbulletin.BusinessRules
{
    //*****************************************************************************
    //  文件名 DeliveryroomstatusbulletinRules.cs
    //*****************************************************************************
    /// <summary>
    /// 数据库表 H_DELIVERYROOMSTATUSBULLETIN BusinessRules层
    /// 主键 INHOSID
    /// </summary>
    /// <remark>
    /// 该表有7个字段
    /// 编制：简易代码生成器
    /// 时间：2013-03-15 09:32:38
    /// </remark>
    public class DeliveryroomstatusbulletinRules : BusinessRulesBase
    {
        #region IDeliveryroomstatusbulletinRules 成员
        
        #region Common

        //公共方法，用于新增、更新、删除及保存
        private int Common(int? otherConnectionStringIndex, Func<IDataAccess, DeliveryroomstatusbulletinDAO, int> func)
        {
            IDataAccess data = GetDataAccessByConnectionStringIndex(otherConnectionStringIndex);

            data.Open();
            SingleTransaction tran = new SingleTransaction(data);
            tran.Begin();
            try
            {
                DeliveryroomstatusbulletinDAO dao = new DeliveryroomstatusbulletinDAO(data);

                //通过委托实现不同方法
                int i = func(data, dao);

                tran.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                throw new LogonException(ex.Message, ex);
            }
            finally
            {
                data.Close();
            }
        }

        #endregion Common
        
        #region Insert

        /// <summary>
        /// 新增
        /// </summary>
        public int Insert(DeliveryroomstatusbulletinData entity)
        {
            return Insert(entity, null);
        }

        /// <summary>
        /// 新增
        /// </summary>
        public int Insert(DeliveryroomstatusbulletinData entity, int? otherConnectionStringIndex)
        {
            return Common(otherConnectionStringIndex, new Func<IDataAccess, DeliveryroomstatusbulletinDAO, int>(delegate(IDataAccess data, DeliveryroomstatusbulletinDAO dao)
                            {
                                int i = dao.Insert(entity);
                                entity.AcceptChange();

                                return i;
                            }
                        ));
        }

        /// <summary>
        /// 新增
        /// </summary>
        public void Insert(EntityList<DeliveryroomstatusbulletinData> list)
        {
            Insert(list, null);
        }

        /// <summary>
        /// 新增
        /// </summary>
        public void Insert(EntityList<DeliveryroomstatusbulletinData> list, int? otherConnectionStringIndex)
        {
            Common(otherConnectionStringIndex, new Func<IDataAccess, DeliveryroomstatusbulletinDAO, int>(delegate(IDataAccess data, DeliveryroomstatusbulletinDAO dao)
                    {
                        foreach (DeliveryroomstatusbulletinData entity in list)
                        {
                            dao.Insert(entity);
                        }

                        list.AcceptChange();
                        return 1;
                    }
                ));
        }

        #endregion Insert
        
        #region Update

        /// <summary>
        /// 更新
        /// </summary>
        public int Update(DeliveryroomstatusbulletinData entity)
        {
            return Update(entity, null);
        }

        /// <summary>
        /// 更新
        /// </summary>
        public int Update(DeliveryroomstatusbulletinData entity, int? otherConnectionStringIndex)
        {
            return Common(otherConnectionStringIndex, new Func<IDataAccess, DeliveryroomstatusbulletinDAO, int>(delegate(IDataAccess data, DeliveryroomstatusbulletinDAO dao)
                            {
                                int i = dao.Update(entity);
                                entity.AcceptChange();

                                return i;
                            }
                        ));
        }

        /// <summary>
        /// 更新
        /// </summary>
        public void Update(EntityList<DeliveryroomstatusbulletinData> list)
        {
            Update(list, null);
        }

        /// <summary>
        /// 更新
        /// </summary>
        public void Update(EntityList<DeliveryroomstatusbulletinData> list, int? otherConnectionStringIndex)
        {
            Common(otherConnectionStringIndex, new Func<IDataAccess, DeliveryroomstatusbulletinDAO, int>(delegate(IDataAccess data, DeliveryroomstatusbulletinDAO dao)
                    {
                        foreach (DeliveryroomstatusbulletinData entity in list)
                        {
                            dao.Update(entity);
                        }

                        list.AcceptChange();
                        return 1;
                    }
                ));
        }

        #endregion Update
        
        #region Delete

        /// <summary>
        /// 删除
        /// </summary>
        public int Delete(string inhosid)
        {
            return Delete(inhosid, null);
        }

        /// <summary>
        /// 删除
        /// </summary>
        public int Delete(string inhosid, int? otherConnectionStringIndex)
        {
            return Common(otherConnectionStringIndex, new Func<IDataAccess, DeliveryroomstatusbulletinDAO, int>(delegate(IDataAccess data, DeliveryroomstatusbulletinDAO dao)
                            {
                                return dao.Delete(inhosid);
                            }
                        ));
        }

        /// <summary>
        /// 删除
        /// </summary>
        public int Delete(DeliveryroomstatusbulletinData entity)
        {
            return Delete(entity.Inhosid);
        }

        /// <summary>
        /// 删除
        /// </summary>
        public int Delete(DeliveryroomstatusbulletinData entity, int? otherConnectionStringIndex)
        {
            int i = Delete(entity.Inhosid, otherConnectionStringIndex);

            return i;
        }

        /// <summary>
        /// 删除
        /// </summary>
        public void Delete(EntityList<DeliveryroomstatusbulletinData> list)
        {
            Delete(list, null);
        }

        /// <summary>
        /// 删除
        /// </summary>
        public void Delete(EntityList<DeliveryroomstatusbulletinData> list, int? otherConnectionStringIndex)
        {
            Common(otherConnectionStringIndex, new Func<IDataAccess, DeliveryroomstatusbulletinDAO, int>(delegate(IDataAccess data, DeliveryroomstatusbulletinDAO dao)
                    {
                        foreach (DeliveryroomstatusbulletinData entity in list)
                        {
                            dao.Delete(entity.Inhosid);
                        }

                        return 1;
                    }
                ));
        }

        #endregion Delete
        
        #region Save

        /// <summary>
        /// 保存
        /// </summary>
        public void Save(DeliveryroomstatusbulletinData entity)
        {
            Save(entity, null);
        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save(DeliveryroomstatusbulletinData entity, int? otherConnectionStringIndex)
        {
            Common(otherConnectionStringIndex, new Func<IDataAccess, DeliveryroomstatusbulletinDAO, int>(delegate(IDataAccess data, DeliveryroomstatusbulletinDAO dao)
                    {
                        switch (entity.EditState)
                        {
                            default:
                            case EntityState.Unchange:
                                return 1;
                            case EntityState.Changed:
                                dao.Update(entity);
                                break;
                            case EntityState.Add:
                                dao.Insert(entity);
                                break;
                            case EntityState.Delete:
                                dao.Delete(entity.Inhosid);
                                break;
                        }

                        entity.AcceptChange();
                        return 1;
                    }
                ));
        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save(EntityList<DeliveryroomstatusbulletinData> list)
        {
            Save(list, null);
        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save(EntityList<DeliveryroomstatusbulletinData> list, int? otherConnectionStringIndex)
        {
            Common(otherConnectionStringIndex, new Func<IDataAccess, DeliveryroomstatusbulletinDAO, int>(delegate(IDataAccess data, DeliveryroomstatusbulletinDAO dao)
                    {
                        foreach (DeliveryroomstatusbulletinData entity in list)
                        {
                            switch (entity.EditState)
                            {
                                default:
                                case EntityState.Unchange:
                                    continue;
                                case EntityState.Changed:
                                    dao.Update(entity);
                                    break;
                                case EntityState.Add:
                                    dao.Insert(entity);
                                    break;
                                case EntityState.Delete:
                                    dao.Delete(entity.Inhosid);
                                    break;
                            }
                        }

                        if (list.DeleteList != null)
                        {
                            foreach (DeliveryroomstatusbulletinData entity in list.DeleteList)
                            {
                                dao.Delete(entity.Inhosid);
                            }
                        }

                        list.AcceptChange();
                        return 1;
                    }
                ));
        }

        #endregion Save
        
        #region GetAll

        /// <summary>
        /// 查询全部
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> GetAll()
        {
            return GetAll(null, null, null);
        }

        /// <summary>
        /// 查询全部
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> GetAll(int? otherConnectionStringIndex)
        {
            return GetAll(null, null, otherConnectionStringIndex);
        }

        /// <summary>
        /// 查询全部
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> GetAll(bool isOrderBy, string context)
        {
            if (isOrderBy)
            {
                return GetAll(context, null, null);
            }
            else
            {
                return GetAll(null, context, null);
            }
        }

        /// <summary>
        /// 查询全部
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> GetAll(bool isOrderBy, string context, int? otherConnectionStringIndex)
        {
            if (isOrderBy)
            {
                return GetAll(context, null, otherConnectionStringIndex);
            }
            else
            {
                return GetAll(null, context, otherConnectionStringIndex);
            }
        }

        /// <summary>
        /// 查询全部
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> GetAll(string orderBy, string sqlText)
        {
            return GetAll(orderBy, sqlText, null);
        }

        /// <summary>
        /// 查询全部
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> GetAll(string orderBy, string sqlText, int? otherConnectionStringIndex)
        {
            IDataAccess data = GetDataAccessByConnectionStringIndex(otherConnectionStringIndex);

            data.Open();

            try
            {
                DeliveryroomstatusbulletinDAO dao = new DeliveryroomstatusbulletinDAO(data);
                return dao.GetAll(orderBy, sqlText);
            }
            catch (Exception ex)
            {
                throw new LogonException(ex.Message, ex);
            }
            finally
            {
                data.Close();
            }
        }

        #endregion GetAll
        
        #region Get

        /// <summary>
        /// 按条件查询
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> Get(string whereClause)
        {
            return Get(whereClause, null, null, null);
        }

        /// <summary>
        /// 按条件查询
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> Get(string whereClause, int? otherConnectionStringIndex)
        {
            return Get(whereClause, null, null, otherConnectionStringIndex);
        }

        /// <summary>
        /// 按条件查询
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> Get(string whereClause, bool isOrderBy, string context)
        {
            if (isOrderBy)
            {
                return Get(whereClause, context, null, null);
            }
            else
            {
                return Get(whereClause, null, context, null);
            }
        }

        /// <summary>
        /// 按条件查询
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> Get(string whereClause, bool isOrderBy, string context, int? otherConnectionStringIndex)
        {
            if (isOrderBy)
            {
                return Get(whereClause, context, null, otherConnectionStringIndex);
            }
            else
            {
                return Get(whereClause, null, context, otherConnectionStringIndex);
            }
        }

        /// <summary>
        /// 按条件查询
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> Get(string whereClause, string orderBy, string sqlText)
        {
            return Get(whereClause, orderBy, sqlText, null);
        }

        /// <summary>
        /// 按条件查询
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> Get(string whereClause, string orderBy, string sqlText, int? otherConnectionStringIndex)
        {
            IDataAccess data = GetDataAccessByConnectionStringIndex(otherConnectionStringIndex);
            
            data.Open();

            try
            {
                DeliveryroomstatusbulletinDAO dao = new DeliveryroomstatusbulletinDAO(data);
                return dao.Get(whereClause, orderBy, sqlText);
            }
            catch (Exception ex)
            {
                throw new LogonException(ex.Message, ex);
            }
            finally
            {
                data.Close();
            }
        }

        #endregion Get
        
        #region GetByPrimaryKey

        /// <summary>
        /// 根据主键查询
        /// </summary>
        public DeliveryroomstatusbulletinData GetByPrimaryKey(string inhosid)
        {
            return GetByPrimaryKey(inhosid, null, null);
        }

        /// <summary>
        /// 根据主键查询
        /// </summary>
        public DeliveryroomstatusbulletinData GetByPrimaryKey(string inhosid, int? otherConnectionStringIndex)
        {
            return GetByPrimaryKey(inhosid, null, otherConnectionStringIndex);
        }

        /// <summary>
        /// 根据主键查询
        /// </summary>
        public DeliveryroomstatusbulletinData GetByPrimaryKey(string inhosid, string sqlText)
        {
            return GetByPrimaryKey(inhosid, sqlText, null);
        }

        /// <summary>
        /// 根据主键查询
        /// </summary>
        public DeliveryroomstatusbulletinData GetByPrimaryKey(string inhosid, string sqlText, int? otherConnectionStringIndex)
        {
            IDataAccess data = GetDataAccessByConnectionStringIndex(otherConnectionStringIndex);

            data.Open();

            try
            {
                DeliveryroomstatusbulletinDAO dao = new DeliveryroomstatusbulletinDAO(data);
                return dao.GetByPrimaryKey(inhosid, sqlText);
            }
            catch (Exception ex)
            {
                throw new LogonException(ex.Message, ex);
            }
            finally
            {
                data.Close();
            }
        }

        #endregion GetByPrimaryKey


        /// <summary>
        /// 获取待产病人
        /// </summary>
        public EntityList<DeliveryroomstatusbulletinData> GetWaits()
        {
            IDataAccess data = GetDataAccessByConnectionStringIndex(null);

            data.Open();

            try
            {
                DeliveryroomstatusbulletinDAO dao = new DeliveryroomstatusbulletinDAO(data);
                return dao.GetWaits();
            }
            catch (Exception ex)
            {
                throw new LogonException(ex.Message, ex);
            }
            finally
            {
                data.Close();
            }
        }
        
        #endregion IDeliveryroomstatusbulletinRules 成员
    }
}

    